<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

import MyColorConfig from '@/components/MyColorConfig.vue'

import {
  getInfraredSetDataApi,
  updateInfraredSetDataApi
} from '@/api/infrared'
// props

// 定义变量
const InfraredConfigVisible = ref(false)
const frequency = ref(0)

const color = ref('')

// 绑定ref
const colorConfigRefs = ref()

// 定义方法
// 展示弹框
const showInfraredconfig = async () => {
  let res:any = await getInfraredSetDataApi()
  console.log('红外配置--->', res)
  
  // if (res.code === -1) {
  //   ElMessage({
  //     // message: `${res.message}`,
  //     message: '网络请求中断获取数据错误！',
  //     type: 'warning'
  //   })
  //   return
  // }
  frequency.value = res.frequency
  InfraredConfigVisible.value = true
}

// 取消按钮
const cancellation = () => {
  InfraredConfigVisible.value = false
}

// 确认按钮
const confirm = async () => {
  if ( !frequency.value || !colorConfigRefs.value.targetColor || (colorConfigRefs.value.targetColor === 'string')) {
    ElMessage({
      message: '请输入配置值！！！',
      type: 'warning'
    })
    return
  }
  let data = {
    frequency: parseInt(frequency.value + ''),
    color: colorConfigRefs.value.targetColor,
    id: 0
  }
  
  let res = await updateInfraredSetDataApi(data)
  console.log('确认配置------>', data, res)
  if (!res.success) {
    ElMessage({
      message: '保存失败！！！',
      type: 'warning'
    })
  } else {
    ElMessage({
      message: '保存成功！！！',
      type: 'success'
    })
  }
  InfraredConfigVisible.value = false
}

defineExpose({
  showInfraredconfig
})
</script>

<template>
  <el-dialog
    v-model="InfraredConfigVisible"
    class="infrared-dialog"
  >
    <template #header>
      <div class="infrared-header-box">
        <div class="title">
          红外配置
        </div>
      </div>
    </template>
    <div class="config-box">
      <div class="left-box">
        <div class="title">
          颜色域配置
        </div>
        <!-- <div class="color-picker">
          <el-color-picker v-model="color" />
        </div> -->
        <!-- <div style="width: 50%;height: 15px;margin-left: 10px;border: 1px solid red;" /> -->
        <MyColorConfig ref="colorConfigRefs" />
      </div>
      <div class="right-box">
        <span>红外扫描仪频率</span>
        <input
          v-model="frequency"
          type="text"
          oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
          class="frequency-box"
        >

        <span>红外录像保存地址</span>
        <div>
          地址......
        </div>
        <span>可见光录像保存地址</span>
        <div>
          地址......
        </div>
      </div>
    </div>
    <div class="btn-box">
      <div
        class="cancellation-btn"
        @click="cancellation"
      />
      <div
        class="confirm-btn"
        @click="confirm"
      />
    </div>
  </el-dialog>
</template>

<style lang="scss" scoped>
.config-box {
  display: flex;
  width: 740px;
  height: 298px;
  background: #fff;
  border: 1px solid #e5e6eb;
  border-radius: 2px;
  opacity: 1;
  .left-box {
    width: 252px;
    height: 280px;

    /* border: 1px solid red; */
    .title {
      width: 70px;
      height: 20px;
      margin-top: 16px;
      margin-left: 20px;
      color: #4e5969;
      font-weight: 500;
      font-size: 14px;
      font-family: PingFang,sans-serif;
      line-height: 16px;
      background-clip: text;

      /* border: 1px solid blue; */
    }
    .color-picker {
      width: 100%;
      height: 20%;
      padding: 10px;
    }
  }
  .right-box {
    width: 488px;
    height: 300px;
    border-left: 1px solid #e5e6eb;
    > span {
      display: block;
      width: 240px;
      height: 20px;
      margin-top: 24px;
      margin-left: 20px;
      color: #4e5969;
      font-weight: 500;
      font-size: 16px;
      font-family: PingFang,sans-serif;
      line-height: 16px;
      background-clip: text;
    }
    > div {
      width: 447px;
      height: 35px;
      margin-top: 10px;
      margin-left: 20px;
      font-size: 16px;
      background: #fff;
      border-radius: 2px;
      opacity: 1;
    }
    .frequency-box {
      width: 200px;
      height: 40px;
      margin-top: 10px;
      margin-left: 20px;
      padding-left: 10px;
      background: #fff;
      border: 1px solid #c9cdd4;
      border-radius: 2px;
      opacity: 1;
    }
  }
}
</style>
<style lang="scss">
.infrared-dialog {
  width: 800px;
  height: 488px;
  .infrared-header-box {
    display: flex;
    .title {
      width: 120px;
      height: 28px;
      color: #4e5969;
      font-weight: 600;
      font-size: 20px;
      font-family: PingFang,sans-serif;
      line-height: 23px;
      background-clip: text;
    }
    .position-name {
      width: 252px;
      height: 28px;
      margin-left: 24px;
      color: #2191ff;
      font-weight: 600;
      font-size: 20px;
      font-family: PingFang,sans-serif;
      line-height: 23px;
      background-clip: text;
    }
  }
  .btn-box {
    display: flex;
    flex-direction: row;
    margin-top: 30px;

    /* border: 1px solid red; */
    .confirm-btn {
      width: 120px;
      height: 42px;
      margin-left: 40px;
      background-image: url("../assets/bckImg/confirm.png");

      /* border: 1px solid red; */
    }
    .confirm-btn:hover {
      cursor: pointer;
    }
    .confirm-btn:active {
      background: rgba($color: #2191ffff, $alpha: 90%);
      cursor: pointer;
    }
    .cancellation-btn {
      width: 120px;
      height: 42px;
      margin-left: 250px;
      background-image: url("../assets/bckImg/cancellation.png");

      /* border: 1px solid red; */
    }
    .cancellation-btn:hover {
      cursor: pointer;
    }
    .cancellation-btn:active {
      background: rgba($color: #a9aeb8ff, $alpha: 90%);
      cursor: pointer;
    }
  }
}
</style>